#!/usr/bin/env python

#    Copyright 2011,2012 Erik Persson
#
#    This file is part of the cell-sync-usrp project.
#
#    cell-sync-usrp is free software: you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation, either version 3 of the License, or
#    (at your option) any later version.
#
#    cell-sync-usrp is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU General Public License
#    along with cell-sync-usrp.  If not, see <http://www.gnu.org/licenses/>.


import pscgen

# generating b from tb and pscgen.a
tb = [1, 1, 1, 1, 1, 1, 1, 1, -1, -1, -1, -1, -1, -1, -1, -1]
b= [];
for i in range(0,16):
    b.append(pscgen.a[i]*tb[i]) # old b=[1, 1, 1, 1, 1, 1, -1, -1, 1, -1, 1, -1, 1, -1, -1, 1]

x = [1, 1, 1, -1, 1, 1, -1, -1, 1, -1, 1, -1, -1, -1, -1, -1]

def combineH(h):
     
    
    xsize = len(h)
    ysize = len(h[0])
    
    """create the 2d array for the new h matrix"""
    nh = range(0, xsize*2)#,ysize*2) );
    
    for i in range(0,xsize*2):
        nh[i] = range(0, ysize*2)
    
    x = 0; 
    while (x<xsize):
        y=0
        while(y<ysize):
            nh[x][y] = h[x][y]
            nh[x+xsize][y] = h[x][y]
            nh[x][y+ysize] = h[x][y]
            nh[x+xsize][y+ysize] = -h[x][y]            
            y=y+1
        x=x+1
        
    return nh
    

def createH(num):
    if (num>0):        
        hprev = createH(num-1)
        return combineH(hprev)
    else:
        return [[1]]
    

def createSimpleSSC(k,h,z):
    m=16*(k-1)
    ssck = []
    i=0
    while i<256:
        ssck.append( h[m][i] * z[i] )
        i=i+1
        
    return ssck

#print combineH([[1]])


if __name__ == '__main__':
    h = createH(8)
    z = pscgen.createSeq(b,x)    
    
    sscs = []
    sscs2 = []
    i=0
    while i<16:
        onessc = createSimpleSSC(i+1,h,z)
        onessc2 = pscgen.mulSeq(onessc,(1+1j))
        onessc = pscgen.mulSeq(onessc,(1-1j))
        onessc2 = pscgen.spreadSeq(onessc2,2)
        onessc = pscgen.spreadSeq(onessc,2)
        
        
        sscs.append(onessc)
        sscs2.append(onessc2)
        i=i+1
    

    f = open("sscseq.h","w")
    f.write("gr_complex *ssc = {\n")
    for onessc in sscs:
        index = 0
        f.write("{")
        while index<len(onessc):
            if (index==len(onessc)-1):
                f.write("gr_complex(" + str(onessc[index].real) + ", " +str(onessc[index].imag) + ")")
                
            else:
                f.write("gr_complex(" + str(onessc[index].real) + ", " +str(onessc[index].imag) + ")")
                f.write(", ")
            index+=1
        f.write("},\n")
    f.write("};")
    
    
    cmd = raw_input("Generate test data (y/n):")
        
    if cmd=='y':
        numRep = int(raw_input("Enter number of repetitions:"))
        
        f = open("sscseq","wb")
        import struct
        
        print "Generating.....",
        rep = 0
        sscNum = 0
        seq = [ 1,  8,  12, 9,  9, \
                4,  13, 16,  5, 1, \
                13, 5, 12,  4, 8]
#        v = 5
#        seq = [v,v,v,v,v, \
#               v,v,v,v,v, \
#               v,v,v,v,v]
        while (rep<numRep):
            i=0
           
            index = seq[sscNum]-1
            while (i<len(sscs2[index])):                
                tmp = struct.pack("ff",sscs2[index][i].real,sscs2[index][i].imag)
                f.write(tmp)
                i=i+1
            while(i<2560*2):
                tmp = struct.pack("ff",0.0,0.0)
                f.write(tmp)
                i=i+1
            rep = rep + 1
            
            sscNum+=1
            if (sscNum>14):
                sscNum=0

        f.close()
        
        print "done!"

